/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.itbaseuser.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.zaxxer.hikari.HikariDataSource;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Transaction;
import vip.xiaonuo.biz.modular.jxrw.entity.JwJxrw;
import vip.xiaonuo.biz.modular.jxrw.param.JwJxrwPageParam;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.itbaseuser.entity.ItBaseUser;
import vip.xiaonuo.biz.modular.itbaseuser.mapper.ItBaseUserMapper;
import vip.xiaonuo.biz.modular.itbaseuser.param.ItBaseUserAddParam;
import vip.xiaonuo.biz.modular.itbaseuser.param.ItBaseUserEditParam;
import vip.xiaonuo.biz.modular.itbaseuser.param.ItBaseUserIdParam;
import vip.xiaonuo.biz.modular.itbaseuser.param.ItBaseUserPageParam;
import vip.xiaonuo.biz.modular.itbaseuser.service.ItBaseUserService;
import vip.xiaonuo.common.util.CommonDbUtil;

import java.util.List;

/**
 * itest用户表Service接口实现类
 *
 * @author lrw
 * @date  2025/09/14 15:02
 **/
@Component
public class ItBaseUserServiceImpl extends ServiceImpl<ItBaseUserMapper, ItBaseUser> implements ItBaseUserService {

    private void jxrwQW(QueryWrapper queryWrapper, ItBaseUserPageParam itBaseUserPageParam){
        if(ObjectUtil.isNotEmpty(itBaseUserPageParam.getUsername())) {
            queryWrapper.like(ItBaseUser::getUsername, itBaseUserPageParam.getUsername());
        }
        if(ObjectUtil.isNotEmpty(itBaseUserPageParam.getUserpass())) {
            queryWrapper.eq(ItBaseUser::getUserpass, itBaseUserPageParam.getUserpass());
        }
        if(ObjectUtil.isNotEmpty(itBaseUserPageParam.getUsercode())) {
            queryWrapper.eq(ItBaseUser::getUsercode, itBaseUserPageParam.getUsercode());
        }
    }
    @Override
    public Page<ItBaseUser> page(ItBaseUserPageParam itBaseUserPageParam) {

        if(ObjectUtil.isEmpty(itBaseUserPageParam.getUsername())&&
                ObjectUtil.isEmpty(itBaseUserPageParam.getUserpass())) {
            return null;
        }
        try {
            HikariDataSource ds = CommonDbUtil.getTmpDs("/itest.properties");
            FlexGlobalConfig.getDefaultConfig().getDataSource().addDataSource("itest", ds);
            DataSourceKey.use("itest");//使用新的数据源
            QueryWrapper queryWrapper = new QueryWrapper();
            jxrwQW(queryWrapper,itBaseUserPageParam);
            if(ObjectUtil.isAllNotEmpty(itBaseUserPageParam.getSortField(), itBaseUserPageParam.getSortOrder())) {
                CommonSortOrderEnum.validate(itBaseUserPageParam.getSortOrder());
                queryWrapper.orderBy(StrUtil.toUnderlineCase(itBaseUserPageParam.getSortField()),itBaseUserPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()));
            } else {
                queryWrapper.orderBy(ItBaseUser::getUid);
            }
            return this.page(CommonPageRequest.defaultPage(), queryWrapper);
        }catch (Exception e){
            return null;
        }finally{
            DataSourceKey.clear();
        }

    }

    @Transaction
    @Override
    public void add(ItBaseUserAddParam itBaseUserAddParam) {
        ItBaseUser itBaseUser = BeanUtil.toBean(itBaseUserAddParam, ItBaseUser.class);
        this.save(itBaseUser);
    }

    @Transaction
    @Override
    public void edit(ItBaseUserEditParam itBaseUserEditParam) {
        ItBaseUser itBaseUser = this.queryEntity(itBaseUserEditParam.getUid());
        BeanUtil.copyProperties(itBaseUserEditParam, itBaseUser);
        this.updateById(itBaseUser);
    }

    /**
     * 重置某人或所有考生的登录密码--> 123456
     * @param itBaseUserPageParam
     * @return
     */

    @Transaction
    @Override
    public boolean resetpwd(ItBaseUserPageParam itBaseUserPageParam) {
        try {
            HikariDataSource ds = CommonDbUtil.getTmpDs("/itest.properties");
            FlexGlobalConfig.getDefaultConfig().getDataSource().addDataSource("itest", ds);
            DataSourceKey.use("itest");//使用新的数据源
            //重置指定某人的密码，可用于修改老师的密码
            if(ObjectUtil.isNotEmpty(itBaseUserPageParam.getUsername())) {
                return UpdateChain.of(ItBaseUser.class)
                        .set(ItBaseUser::getUserpass,"E10ADC3949BA59ABBE56E057F20F883E")
                        .where(ItBaseUser::getUserpass).ne("E10ADC3949BA59ABBE56E057F20F883E")
                        .and(ItBaseUser::getUsername).eq(itBaseUserPageParam.getUsername())
                        //.and(QueryMethods.length(ItBaseUser::getUsername).eq(8).or(QueryMethods.length(ItBaseUser::getUsername).eq(9)))
                        .update();
            }else {//全部重置
                return UpdateChain.of(ItBaseUser.class)
                        .set(ItBaseUser::getUserpass, "E10ADC3949BA59ABBE56E057F20F883E")
                        .where(ItBaseUser::getUserpass).ne("E10ADC3949BA59ABBE56E057F20F883E")
                        .and(QueryMethods.length(ItBaseUser::getUsername).eq(8).or(QueryMethods.length(ItBaseUser::getUsername).eq(9)))
                        .update();
            }

        }catch (Exception e){
            return false;
        }finally{
            DataSourceKey.clear();
        }

    }
    @Transaction
    @Override
    public void delete(List<ItBaseUserIdParam> itBaseUserIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(itBaseUserIdParamList, ItBaseUserIdParam::getUid));
    }

    @Override
    public ItBaseUser detail(ItBaseUserIdParam itBaseUserIdParam) {
        return this.queryEntity(itBaseUserIdParam.getUid());
    }

    @Override
    public ItBaseUser queryEntity(String id) {
        ItBaseUser itBaseUser = this.getById(id);
        if(ObjectUtil.isEmpty(itBaseUser)) {
            throw new CommonException("itest用户表不存在，id值为：{}", id);
        }
        return itBaseUser;
    }
    @Override
    public ItBaseUser queryEntity(Integer id) {
        ItBaseUser itBaseUser = this.getById(id);
        if(ObjectUtil.isEmpty(itBaseUser)) {
            throw new CommonException("itest用户表不存在，id值为：{}", id);
        }
        return itBaseUser;
    }
}
